#include "Component\UI\TransformUI.h"
#include "TransformUI.h"
using namespace SakuraAge;
Vector2 SakuraAge::TransformUI::GetPosition() {
  if (rotation != 0) {
    DebugLogError("[TransformUI] Rotation are not zero ,there may be some unexcept situactions.\n In Object" + item->name + "\n");
  }
  if (father != nullptr) {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.father->position + TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += TopLeft.father->scale.y;
          case 8:
            p1.y += TopLeft.father->scale.y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += TopLeft.father->scale.x;
          case 2:
            p1.x += TopLeft.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = TopLeft.father->position +
             Vector2(TopLeft.father->scale.x * TopLeft.position.x,
                     TopLeft.father->scale.y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.father->position + BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += BottomRight.father->scale.y;
          case 8:
            p2.y += BottomRight.father->scale.y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += BottomRight.father->scale.x;
          case 2:
            p2.x += BottomRight.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = BottomRight.father->position +
             Vector2(BottomRight.father->scale.x * BottomRight.position.x,
                     BottomRight.father->scale.y * BottomRight.position.y);
    }
    return (p1 + p2) / 2;
  } else {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += RendererControl::WSize->y;
          case 8:
            p1.y += RendererControl::WSize->y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += RendererControl::WSize->x;
          case 2:
            p1.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = Vector2(RendererControl::WSize->x * TopLeft.position.x,
                     RendererControl::WSize->y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += RendererControl::WSize->y;
          case 8:
            p2.y += RendererControl::WSize->y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += RendererControl::WSize->x;
          case 2:
            p2.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = Vector2(RendererControl::WSize->x * BottomRight.position.x,
                     RendererControl::WSize->y * BottomRight.position.y);
    }
    return (p1 + p2) / 2;
  }
}

Vector2 SakuraAge::TransformUI::GetScale() {
  if (father != nullptr) {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.father->position + TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += TopLeft.father->scale.y;
          case 8:
            p1.y += TopLeft.father->scale.y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += TopLeft.father->scale.x;
          case 2:
            p1.x += TopLeft.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = TopLeft.father->position +
             Vector2(TopLeft.father->scale.x * TopLeft.position.x,
                     TopLeft.father->scale.y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.father->position + BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += BottomRight.father->scale.y;
          case 8:
            p2.y += BottomRight.father->scale.y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += BottomRight.father->scale.x;
          case 2:
            p2.x += BottomRight.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = BottomRight.father->position +
             Vector2(BottomRight.father->scale.x * BottomRight.position.x,
                     BottomRight.father->scale.y * BottomRight.position.y);
    }
    return (p2 - p1);
  } else {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += RendererControl::WSize->y;
          case 8:
            p1.y += RendererControl::WSize->y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += RendererControl::WSize->x;
          case 2:
            p1.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = Vector2(RendererControl::WSize->x * TopLeft.position.x,
                     RendererControl::WSize->y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += RendererControl::WSize->y;
          case 8:
            p2.y += RendererControl::WSize->y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += RendererControl::WSize->x;
          case 2:
            p2.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = Vector2(RendererControl::WSize->x * BottomRight.position.x,
                     RendererControl::WSize->y * BottomRight.position.y);
    }
    return (p2 - p1);
  }
}

void SakuraAge::TransformUI::SetLU(Vector2 p, PositionType t,
                                   ObjRef<Transform> f, Part part) {
  TopLeft.position = p;
  TopLeft.pType = t;
  TopLeft.father = f;
  TopLeft.part = part;
}

void SakuraAge::TransformUI::SetRD(Vector2 p, PositionType t,
                                   ObjRef<Transform> f, Part part) {
  BottomRight.position = p;
  BottomRight.pType = t;
  BottomRight.father = f;
  BottomRight.part = part;
}

void SakuraAge::TransformUI::OnWindowSizeChange() {
  if (father != nullptr) {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.father->position + TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += TopLeft.father->scale.y;
          case 8:
            p1.y += TopLeft.father->scale.y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += TopLeft.father->scale.x;
          case 2:
            p1.x += TopLeft.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = TopLeft.father->position +
             Vector2(TopLeft.father->scale.x * TopLeft.position.x,
                     TopLeft.father->scale.y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.father->position + BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += BottomRight.father->scale.y;
          case 8:
            p2.y += BottomRight.father->scale.y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += BottomRight.father->scale.x;
          case 2:
            p2.x += BottomRight.father->scale.x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = BottomRight.father->position +
             Vector2(BottomRight.father->scale.x * BottomRight.position.x,
                     BottomRight.father->scale.y * BottomRight.position.y);
    }
    position = (p1 + p2) / 2;
    scale = (p2 - p1);
  } else {
    Vector2 p1, p2;

    switch (TopLeft.pType) {
      case PositionType::Attach:
        p1 = TopLeft.position;
        switch (TopLeft.part << 2) {
          case 4:
            p1.y += RendererControl::WSize->y;
          case 8:
            p1.y += RendererControl::WSize->y / 2;
        }
        switch (TopLeft.part >> 2) {
          case 1:
            p1.x += RendererControl::WSize->x;
          case 2:
            p1.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p1 = Vector2(RendererControl::WSize->x * TopLeft.position.x,
                     RendererControl::WSize->y * TopLeft.position.y);
    };

    switch (BottomRight.pType) {
      case PositionType::Attach:
        p2 = BottomRight.position;
        switch (BottomRight.part << 2) {
          case 4:
            p2.y += RendererControl::WSize->y;
          case 8:
            p2.y += RendererControl::WSize->y / 2;
        }
        switch (BottomRight.part >> 2) {
          case 1:
            p2.x += RendererControl::WSize->x;
          case 2:
            p2.x += RendererControl::WSize->x / 2;
        }
        break;
      case PositionType::Scale:
        p2 = Vector2(RendererControl::WSize->x * BottomRight.position.x,
                     RendererControl::WSize->y * BottomRight.position.y);
    }
    position = (p1 + p2) / 2;
    scale = (p2 - p1);
  }
}
